home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / BLAKJACK.ARJ / 072392B.BAK next >
Text File  |  1992-09-08  |  12KB  |  220 lines

  1. /*  Project # 4  b) EXTRA CREDIT  A game of Blackjack w/o shuffling  */
  2.  
  3. /*  David Candel For Dr. Beheshti in Unix And C, August 1, 1992  */
  4.  
  5. #include<ctype.h>
  6. #include<stdio.h>                               /*  preprocessor directives */
  7. #include<stdlib.h>
  8. struct deck              /*  declare a structure to hold the deck of cards  */
  9. {
  10.   int  card_number, value, taken;
  11.   char suit_rank[2];                /*  an array to hold the card suit and  */
  12.   };                                                 /*  and the card rank  */
  13.  
  14. main ()                                                           /*  main  */
  15. {
  16.  int a, temp, first, second, i, j, count, value3, value4, sum1, sum2, flag = 0, ace;
  17.  char choice1, choice2;                        /*  definition of variables  */
  18.  float seed;
  19.  
  20. struct deck deck[51] =         /*  structure to hold the card number, card  */
  21. { { 1, 1, 0, {'H', 'A'} },             /*  value, card suit, and card rank  */
  22.   { 2, 10, 0, {'H', 'K'} },                /*  definition of the structure  */
  23.   { 3, 10, 0, {'H', 'Q'} },                  /*  allocates space in memory  */
  24.   { 4, 10, 0, {'H', 'J'} },                          /*  contains an array  */
  25.   { 5, 10, 0, {'H', 'T'} },                                 /*  of size 52  */
  26.   { 6, 9, 0, {'H', 'N'} },
  27.   { 7, 8, 0, {'H', 'E'} },
  28.   { 8, 7, 0, {'H', 'S'} },
  29.   { 9, 6, 0, {'H', 'X'} },
  30.   { 10, 5, 0, {'H', 'F'} },
  31.   { 11, 4, 0, {'H', 'R'} },
  32.   { 12, 3, 0, {'H', 'H'} },
  33.   { 13, 2, 0, {'H', 'W'} },
  34.   { 14, 1, 0, {'D', 'A'} },
  35.   { 15, 10, 0, {'D', 'K'} },
  36.   { 16, 10, 0, {'D', 'Q'} },
  37.   { 17, 10, 0, {'D', 'J'} },
  38.   { 18, 10, 0, {'D', 'T'} },
  39.   { 19, 9, 0, {'D', 'N'} },
  40.   { 20, 8, 0, {'D', 'E'} },
  41.   { 21, 7, 0, {'D', 'S'} },
  42.   { 22, 6, 0, {'D', 'X'} },
  43.   { 23, 5, 0, {'D', 'F'} },
  44.   { 24, 4, 0, {'D', 'R'} },
  45.   { 25, 3, 0, {'D', 'H'} },
  46.   { 26, 2, 0, {'D', 'W'} },
  47.   { 27, 1, 0, {'C', 'A'} },
  48.   { 29, 10, 0, {'C', 'Q'} },
  49.   { 30, 10, 0, {'C', 'J'} },
  50.   { 31, 10, 0, {'C', 'T'} },
  51.   { 32, 9, 0, {'C', 'N'} },
  52.   { 33, 8, 0, {'C', 'E'} },
  53.   { 34, 7, 0, {'C', 'S'} },
  54.   { 35, 6, 0, {'C', 'X'} },
  55.   { 36, 5, 0, {'C', 'F'} },
  56.   { 37, 4, 0, {'C', 'R'} },
  57.   { 38, 3, 0, {'C', 'H'} },
  58.   { 39, 2, 0, {'C', 'W'} },
  59.   { 40, 1, 0, {'S', 'A'} },
  60.   { 41, 10, 0, {'S', 'K'} },
  61.   { 42, 10, 0, {'S', 'Q'} },
  62.   { 43, 10, 0, {'S', 'J'} },
  63.   { 44, 10, 0, {'S', 'T'} },
  64.   { 45, 9, 0, {'S', 'N'} },
  65.   { 46, 8, 0, {'S', 'E'} },
  66.   { 47, 7, 0, {'S', 'S'} },
  67.   { 48, 6, 0, {'S', 'X'} },
  68.   { 49, 5, 0, {'S', 'F'} },
  69.   { 50, 4, 0, {'S', 'R'} },
  70.   { 51, 3, 0, {'S', 'H'} },
  71.   { 52, 2, 0, {'S', 'W'} } };
  72. printf ("Welcome to B L A C K J A C K . . .\n");         /*  opening title  */
  73. printf ("Pick a seed number:");         /*  opportunity to set the seed in  */
  74. fflush(stdin);
  75. scanf ("%f", &seed);                               /*  the random function  */
  76. printf ("\nWelcome!!! Shortly you will encounter a superb game but...");
  77. printf ("first let me explain that each player is dealt a random card on");
  78. printf ("each player's turn and the first number for each player is the card");
  79. printf ("number from one to fifty-two, fifty-two being the number of cards in");
  80. printf ("a deck. Immediately following this number is the card suit and then the");
  81. printf ("card rank. These values are abbreviated but you can guess them right.");
  82. printf ("The last number is the card's value which we hope will add in the");
  83. printf ("player's behalf to 21! Here we go and have fun!");
  84. srand (seed);                                 /*  function to set the seed  */
  85. taken_loop1: i = rand ()%52 + 1;  /*  random card generator used later too  */
  86.  deck[i].taken = 1;                             /*  this card is now taken  */
  87.  if ((deck[i].card_number == 1) || (deck[i].card_number == 14) || (deck[i].card_number == 27) || (deck[i].card_number == 40))
  88.  {                                                                  /*  if  */
  89.  printf ("card value = %d\n", deck[i].value);/*  report the face value oface*/
  90.  printf ("Would you like this card (ace) to be valued at 1 or 11? ");
  91.  scanf ("%d", &ace);             /*  asks player # 1 for value of this ace  */
  92.  deck[i].value = ace;                        /*  new face value of the ace  */
  93.  }
  94.  printf ("Player # 1 has a: %d %c%c %d\n", deck[i].card_number, deck[i].suit_rank[0], deck[i].suit_rank[1], deck[i].value);
  95. sum1 = deck[i].value;   /*  sum1 sets original sum of first card after the  */
  96.  i = rand ()%52 + 1;    /*  random card is selected after seed and cutting  */
  97.  if (deck[i].taken)           /*  check to see if the card is already used  */
  98.  {
  99.   printf ("card already taken %d ", deck[i].card_number); /*  write the used*/
  100.   goto taken_loop1;  /*  card number to screen and proceed to pick another  */
  101.  }
  102.  else
  103.  deck[i].taken = 1;                             /*  this card is now taken  */
  104.  if ((deck[i].card_number == 1) || (deck[i].card_number == 14) || (deck[i].card_number == 27) || (deck[i].card_number == 40))
  105.  {                                                                 /*  if  */
  106.  printf ("card value = %d\n", deck[i].value);/*  report the face value oface*/
  107.  printf ("Would you like this card (ace) to be valued at 1 or 11? ");
  108.  scanf ("%d", &ace);             /*  asks player # 1 for value of this ace  */
  109.  deck[i].value = ace;                        /*  new face value of the ace  */
  110.  }
  111. printf ("Player # 2 has a: %d %c%c %d\n", deck[i].card_number, deck[i].suit_rank[0], deck[i].suit_rank[1], deck[i].value);
  112. sum2 = deck[i].value;              /*  sum2 sets sum for player number two  */
  113. while ((sum2 < 22 && sum1 < 22) || (sum1 == 21 || sum2 == 21) || (choice1 && choice2 == 'n'))
  114. {                                                                /*  while  */
  115. printf ("Player # 1: Do you want another card? y/n\n");  /*  computer asks  */
  116. fflush(stdin);                                /*  flush buffer in keyboard  */
  117. choice1 = getchar ();                      /*  receive from keyboard input  */
  118. choice1 = tolower(choice1);                             /*  make lowercase  */
  119. if (choice1 == 'y')                                                 /*  if  */
  120. {                                      /*  card is chosen but not taken  */
  121. taken_loop2: i = rand ()%52 + 1;         /* test in a loop a random number  */
  122.  if (deck[i].taken)                                                 /*  if  */
  123.  {
  124.   printf ("card already taken %d", deck[i].card_number);/*  write the used  */
  125.   goto taken_loop2;                    /* receive a new card from the deck  */
  126.  }                                                 /* if card is new go on  */
  127.  else                                                             /*  else  */
  128.  deck[i].taken = 1;                             /*  this card is now taken  */
  129.   if ((deck[i].card_number == 1) || (deck[i].card_number == 14) || (deck[i].card_number == 27) || (deck[i].card_number == 40))
  130.   {                                                                 /*  if  */
  131.  printf ("card value = %d\n", deck[i].value);/*  report the face value oface*/
  132.  printf ("Would you like this card (ace) to be valued at 1 or 11? ");
  133.  scanf ("%d", &ace);             /*  asks player # 1 for value of this ace  */
  134.  deck[i].value = ace;                        /*  new face value of the ace  */
  135.  }
  136. printf ("Player # 1 has a: %d %c%c %d also\n", deck[i].card_number, deck[i].suit_rank[0], deck[i].suit_rank[1], deck[i].value);
  137. value3 = deck[i].value;  /*  assign a temp variable the face value of card  */
  138. sum1 = sum1 + value3;                          /*  update the player's sum  */
  139. printf ("Player # 1: Your sum now is: %d\n", sum1);         /*  report sum  */
  140. if (sum1 == 21)                                                     /*  if  */
  141. {
  142.  taken_loopspecial: i = rand ()%52 + 1; /* special loop if # 1 player = 21  */
  143.  if (deck[i].taken)                         /* test for a new or used card  */
  144.   {
  145.   printf ("card already taken %d", deck[i].card_number);
  146.   goto taken_loopspecial;/* goto special loop when player #2 gets a new card*/
  147.  }
  148.  else printf ("Player # 2 has a %d %c%c %d also\n", deck[i].card_number, deck[i].suit_rank[0], deck[i].suit_rank[1], deck[i].value);
  149.  value4 = deck[i].value; /*  assign a temp variable the face value of card  */
  150.  sum2 = sum2 + value4;                         /*  update the player's sum  */
  151.  if (sum2 == 21)                                                    /*  if  */
  152.  printf ("Game is declared a tie at 21 all!!!!");/*  both sums were tested  */
  153.  else printf ("Player # 1 wins blackjack!");     /*  else Player #1 has 21  */
  154. exit (EXIT_SUCCESS);                                 /*  exit successfully  */
  155. }
  156. if (sum1 > 21)                                                      /*  if  */
  157. {
  158.  printf ("Player # 2 wins blackjack!");    /* Player # 1's sum exceeded 21  */
  159.  exit (EXIT_SUCCESS);                                /*  exit successfully  */
  160. }
  161. }
  162. else                                                              /*  else  */
  163. {
  164. printf ("Player # 1 stands.");  /*  if player # 1 wants no card this round  */
  165. flag = flag + 1;/*  flag variable to see if both players want no more cards */
  166. if (flag == 2)                           /*  if so break out of while loop  */
  167. break;
  168. }
  169.  
  170. printf ("Player # 2: Do you want another card? y/n\n");/*  Player # 2's turn*/
  171. fflush(stdin);                                          /*  flush keyboard  */
  172. choice2 = getchar ();                   /*  receive decision from keyboard  */
  173. choice2 = tolower(choice2);                             /*  make lowercase  */
  174. if (choice2 == 'y')                                                 /*  if  */
  175. {
  176. taken_loop3: i = rand ()%52 + 1;        /*  test in a loop a random number  */
  177.  if (deck[i].taken)                  /*  see if card has already been used  */
  178.  {
  179.   printf ("card already taken %d", deck[i].card_number);/* write used number*/
  180.   goto taken_loop3;                   /*  receive a new card from the deck  */
  181.  }
  182.  else                                                             /*  else  */
  183.  deck[i].taken = 1;                             /*  this card is now taken  */
  184.  if ((deck[i].card_number == 1) || (deck[i].card_number == 14) || (deck[i].card_number == 27) || (deck[i].card_number == 40))
  185.  {                                                                  /*  if  */
  186.  printf ("card value = %d\n", deck[i].value);/*  report the face value oface*/
  187.  printf ("Would you like this card (ace) to be valued at 1 or 11? ");
  188.  scanf ("%d", &ace);             /*  asks player # 1 for value of this ace  */
  189.  deck[i].value = ace;                        /*  new face value of the ace  */
  190.  }
  191.  printf ("Player # 2 has a: %d %c%c %d also\n", deck[i].card_number, deck[i].suit_rank[0], deck[i].suit_rank[1], deck[i].value);
  192. value4 = deck[i].value;/* assign a temp variable the face value of the card */
  193. sum2 = sum2 + value4;                     /*  update the sum of Player # 2  */
  194. printf ("Player # 2: Your sum now is: %d\n", sum2);  /*  report sum of # 2  */
  195. if (sum2 > 21)                                                      /*  if  */
  196. {
  197.  printf ("Player # 1 wins blackjack!"); /*  test if Player # 2 exceeded 21  */
  198.  exit (EXIT_SUCCESS);                                /*  exit successfully  */
  199.  }
  200.  else if (sum2 == 21)                                          /*  else if  */
  201.  {
  202.   printf ("Player # 2 wins.\n"); /*  Player # 2 went second so is the final */
  203.   exit (EXIT_SUCCESS);             /*  qualifier  */ /*  exit successfully  */
  204.   }
  205. }
  206. else                                                              /*  else  */
  207. {
  208. printf ("Player # 2 stands."); /*  Player # 2 wants no more cards this round*/
  209. flag = flag + 1;/*  flag variable to see if both players want no more cards */
  210. if (flag == 2)                           /*  if so break out of while loop  */
  211. break;
  212. }
  213. }                                                                /*  while  */
  214. if (sum1 > sum2)      /*  see if player # 1 has higher sum after both want  */
  215. printf ("Player # 1 is victorious!");          /*  no more cards once each  */
  216. if (sum2 > sum1)      /*  see if player # 2 has higher sum after both want  */
  217. printf ("Player # 2 is victorious!");          /*  no more cards once each  */
  218. }                                                                 /*  main  */
  219.  
  220.